/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package main;

import tst.TST;
import util.CommonUtil;
import util.FileUtility;

/**
 *
 * @author omega
 */
public class EngineData {
    public static final String DB_GI="file:///root1/db_gi.dat";
    public static final String DB_IG="file:///root1/db_ig.dat";
    public TST tst;
    public static final byte ORI_GAUL_IND=0;
    public static final byte ORI_IND_GAUL=1;

    public EngineData() {
        tst = new TST();
        tst.buildTree();
    }

    public void loadData(byte ori){
        System.out.println("START LOAD DATA");
        String data="";
        if(ori==ORI_GAUL_IND){
            data = FileUtility.readFileCompressed(DB_GI);
        }else if(ori==ORI_IND_GAUL){
            data = FileUtility.readFileCompressed(DB_IG);
        }
        //split data
        String [] datas = CommonUtil.splitStr(data, "\n");
        //force to remove data from memory
        data=null;
        System.gc();

        //is needed?
//        tst = new TST();
//        tst.buildTree();

        String []pairData=null;
        for (int i = 0; i < datas.length; i++) {
            pairData = CommonUtil.splitStr(datas[i], "=");
            tst.insert(pairData[0], pairData[1]);
            System.out.println(i+":"+pairData[0]);
        }
        datas=null;
        pairData=null;
        System.gc();
        System.out.println("END LOAD DATA");
        //stop splash
        KamusMidlet.midlet.splash.stopTimer();
    }

    public String search(String key){
        System.out.println("SEARCH DATA");
        return (String)tst.getValue(key);
    }
}
